package com.ssi.myshop.Presenter.impl;

import android.util.Log;

import com.ssi.myshop.Presenter.IDetailPresenter;
import com.ssi.myshop.base.BaseData;
import com.ssi.myshop.base.BaseObserver;
import com.ssi.myshop.bean.DetailBean;
import com.ssi.myshop.contants.Api;
import com.ssi.myshop.utils.RetrofitManager;
import com.ssi.myshop.utils.RxUtil;
import com.ssi.myshop.view.IDetailCallBack;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class IDetailPresenterImpl implements IDetailPresenter {

    private static final String TAG = "IDetailPresenterImpl";

    private IDetailCallBack mCallBack = null;
    private Retrofit mRetrofit;
    private Api mApi;

    @Override
    public void getDetailById(int id) {
        Log.d(TAG, "getDetailById: "+id);
        mRetrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = mRetrofit.create(Api.class);
        Observable<BaseData<DetailBean>> observable = mApi.getDetailById(id);
        observable.compose(RxUtil.schedulerTransformer()).subscribeWith(new BaseObserver<DetailBean>(mCallBack) {
            @Override
            protected void onSuccess(DetailBean data) {
                mCallBack.onGetDetailSuccess(data);
            }
        });
    }

    @Override
    public void registerViewCallBack(IDetailCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void unregisterViewCallBack(IDetailCallBack callBack) {
        if (mCallBack != null) {
            mCallBack = null;
        }
    }
}
